<?php


@set_time_limit( 0 );
require_once( "_inc.php" );
$dataDir = "../#data/db/";
$action = !empty( $_GET['action'] ) ? $_GET['action'] : "";
if ( "import" == $action && !empty( $_GET['file'] ) )
{
	$filename = $_GET['file'];
	$oriFilename = "";
	if ( eregi( ".zip\$", $filename ) )
	{
		$oriFilename = $filename;
		$filename = eregi_replace( ".zip\$", ".sql", $filename );
		require_once( $dRootDir."inc/classes/phpzip.php" );
		$zip = new phpzip( );
		$zip->unzip( $dataDir.$oriFilename, $dataDir );
	}
	if ( !( $fp = fopen( $dataDir.$filename, "r" ) ) )
	{
		exit( "File not found." );
	}
	$sql = "";
	while ( !feof( $fp ) )
	{
		$line = trim( fgets( $fp, 524288 ) );
		if ( ereg( ";\$", $line ) )
		{
			$sql .= $line;
			$db->query( $sql );
			$sql = "";
		}
		else if ( !ereg( "^(//|--)", $line ) )
		{
			$sql .= $line;
		}
	}
	fclose( $fp );
	if ( !empty( $oriFilename ) )
	{
		unlink( $dataDir.$filename );
	}
	$tpl->assign( "msg", "数据恢复成功！" );
	$tpl->assign( "backUrl", "db_import.php" );
	$tpl->display( "_msg.tpl" );
	exit( );
}
if ( "delete" == $action && !empty( $_GET['file'] ) || unlink( $dataDir.$_GET['file'] ) )
{
	header( "location: ?" );
	exit( );
}
$arrSqls = array( );
if ( $handle = @opendir( $dataDir ) )
{
	while ( FALSE !== ( $file = readdir( $handle ) ) )
	{
		if ( is_file( $dataDir.$file ) )
		{
			$arrSqls[] = array(
				"file" => $file,
				"size" => filesize( $dataDir.$file )
			);
		}
	}
	closedir( $handle );
}
$tpl->assign( "arr", $arrSqls );
$tpl->display( );
?>
